mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					up: {
						auto:false,
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
	});

	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var currenPage=1;
    var pageSize=8;
    var total_page=0;
    var total_count=0;
    var brand_id=0;
    var goods_type=0;
    var suppliers_id=0;
    var search_name='';
    var store_id=0;
    
	refresh_data();
	
	mui.plusReady(function() {
	
		mui.ajax({
			url: apiurl + "Store/get_goods_types/",
			data: {'_token':userinfo.token},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				//console.log(JSON.stringify(res));
				if(res.status ==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				}
				
				// 请求成功
				var obj = res.store_types;
				var htm = '';
				for(z in obj) {
					htm += '<li data-id="' + obj[z]['id'] + '">' + obj[z]['name'] + '</li>';
				}
				htm += '<div style="clear: both;"></div>';
				$('#shop_type #store_type').append(htm);
	
				obj = res.store_business;
				htm = '';
				for(z in obj) {
					htm += '<li data-id="' + obj[z]['id'] + '">' + obj[z]['name'] + '</li>';
				}
				htm += '<div style="clear: both;"></div>';
				$('#shop_type #store_business').append(htm);
	
				obj = res.store_brands;
				htm = '';
				for(z in obj) {
					htm += '<li data-id="' + obj[z]['id'] + '">' + obj[z]['name'] + '</li>';
				}
				htm += '<div style="clear: both;"></div>';
				$('#shop_type #store_brand').append(htm);
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('操作失败！');
			}
		});
	
		document.getElementById('show_menu').addEventListener('tap', function() {
			if(document.getElementById('shop_type').style.display == 'none') {
				$("#shop_type").slideDown("fast");
			} else {
				$("#shop_type").slideUp("fast");
			}
		}, false);
	
		mui("#shop_type").on('tap', '#store_type li', function() {
			if(this.className == "selected") {
				this.className = "";
				goods_type = 0;
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			} else {
				var arr_data = document.querySelectorAll('#shop_type #store_type li');
				mui.each(arr_data, function(index, item) {
					item.className = "";
				});
	
				this.className = "selected";
				goods_type = this.getAttribute('data-id');
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			}
		})
	
		mui("#shop_type").on('tap', '#store_business li', function() {
			if(this.className == "selected") {
				this.className = "";
				suppliers_id = 0;
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			} else {
				var arr_data = document.querySelectorAll('#shop_type #store_business li');
				mui.each(arr_data, function(index, item) {
					item.className = "";
				});
	
				this.className = "selected";
				suppliers_id = this.getAttribute('data-id');
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			}
		})
	
		mui("#shop_type").on('tap', '#store_brand li', function() {
			if(this.className == "selected") {
				this.className = "";
				brand_id = 0;
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			} else {
				var arr_data = document.querySelectorAll('#shop_type #store_brand li');
				mui.each(arr_data, function(index, item) {
					item.className = "";
				});
	
				this.className = "selected";
				brand_id = this.getAttribute('data-id');
				mui('#pullrefresh').pullRefresh().refresh(true);
				refresh_data();
			}
		})
	
		document.getElementById('do_search').addEventListener('tap', function() {
			var keyword = document.getElementById('keyword').value.trim();
			if(keyword == '' && search_name == '') {
				mui.alert('请输入商品名称，再进行搜索');
				return false;
			}
			search_name = keyword;
			mui('#pullrefresh').pullRefresh().refresh(true);
			refresh_data();
		}, false);
	
		document.getElementById('keyword').addEventListener('input', function() {
			var keyword = this.value.trim();
			if(keyword == '' && search_name == '') {
				return false;
			}
			search_name = keyword;
			mui('#pullrefresh').pullRefresh().refresh(true);
			refresh_data();
		}, false);
		
		window.addEventListener('refresh_data', function(event) {
			//获得事件参数
			store_id = event.detail.store_id;
			var store_name = event.detail.store_name;
			var htm = '<h1 class="mui-title" style="width: 60%;height: 35px;background: black;top:19px;line-height: 35px;">';
			htm += '<span id="store_name" style="color: rgb(250,175,58);font-size: 17px;">' + store_name + '</span></h1>';
			htm += '<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>';
			document.querySelector('.mui-bar.mui-bar-nav.home_top').innerHTML = htm;
			refresh_data();
		});
	
		mui(".mui-table-view").on('tap', '.mui-table-view-cell a', function() {
			var goods_id = this.getAttribute("data-id");
			//打开商品详情页
			mui.openWindow({
				id: 'commodity_detail.html',
				url: 'pages/shop/commodity_detail.html',
				extras: {
					goods_id: goods_id //自定义扩展参数，可以用来处理页面间传值
				},
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		})
	
	});


    
    
    function refresh_data(){
    	currenPage=1;
    	mui.ajax({
				url: apiurl + "Store/goods_list/",
				data: {'p':currenPage,'size':pageSize,'brand_id':brand_id,'goods_type':goods_type,'suppliers_id':suppliers_id,'keyword':search_name,'store_id': store_id,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						if($('#pullrefresh .mui-scroll .mui-table-view li').length>0){
							$('#pullrefresh .mui-scroll .mui-table-view').empty();
						}
						
						total_page=res.total_page;
						total_count=res.sum;
						var htm=get_data(res.resource);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
						console.log(htm);
						
					}
					//mui('#pullrefresh').pullRefresh().refresh(true);
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
    }
    
    
    function get_data(data){
    	var htm='';
		for(i in data) {
			/*
			 * <li class="mui-table-view-cell"">
						<a>
							<div class="commodity_img"><img src="../../img/shop.png"></div>
							<div class="commodity_main">
								<p>苹果iphone7 全网通4G手机</p>
								<h5>5692</h5>
							</div>
						</a>
					</li>
			 */
			htm += '<li class="mui-table-view-cell">';
			htm+='<a class="see_good" data-id="'+data[i]['goods_id']+'">';
			htm+='<div class="commodity_img">';
			if(data[i]['original_img'].indexOf(upurl+'Uploads') !== -1){
				var img_path=data[i]['original_img'];
			}
			else{
				var img_path=upurl+data[i]['original_img'];
			}
			htm+='<img src="'+img_path+'">';
			htm+='</div>';
			htm+='<div class="commodity_main">';
			htm+='<p>'+data[i]['goods_name']+'</p>';
			htm+='<h5>'+data[i]['shop_price']+'</h5>';
			htm+='</div>';
			htm+='</a>';
			htm+='</li>';
		}
		return htm;
    }


function pullupRefresh() {
		setTimeout(function() {
			currenPage++;
			mui.ajax({
				url: apiurl + "Store/goods_list/",
				data: {'p':currenPage,'size':pageSize,'brand_id':brand_id,'goods_type':goods_type,'suppliers_id':suppliers_id,'keyword':search_name,'store_id': store_id,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						var htm=get_data(res.resource);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
					}
					
					if(currenPage < total_page){
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    /*能上拉*/
	                }else{
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);/*不能上拉*/
	                }
					
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
		}, 1500);
	}
	
